scheduler = {};

function scheduler:Start()

    Phase = SR_RACING
    PhaseCounter = SR_RACE_TIME
    StartTime = 0
    PhaseLength = SR_RACE_TIME
    
    --self:FindDoors()

    self:UpdateUmsg()
    
    timer.Create("CounterTick", 1, 0, CounterTick)
end

function scheduler:UpdateUmsg()

    umsg.Start( "PhaseCounter" )
        umsg.Long( PhaseCounter )
    umsg.End()
    
    umsg.Start( "Phase" )
        umsg.Long( Phase )
    umsg.End()
    
    umsg.Start( "PhaseLength" )
        umsg.Long( PhaseLength )
    umsg.End()
    
    --umsg.Start( "StartTime" )
    --    umsg.Float( StartTime )
    --umsg.End()
    
    umsg.Start( "ServerSysTime" )
        umsg.Float( SysTime() )
    umsg.End()

end

function UpdateSplit()
    umsg.Start( "ServerSysTime" )
        umsg.Float( SysTime() )
    umsg.End()
end

function scheduler:FindDoors()

    local doors = ents.FindByClass( "func_door*" )
    print("======= "..#doors.." Doors Found =========")
    local checkpoints = 0
    for k,v in pairs (doors) do
        --if v:GetName() == "StartBarrier" || v:GetName() == "start_barrier" then
        if string.lower(string.Left(v:GetName(),5)) == "start" then
            start_barrier = v
            SetGlobalEntity ( "start_barrier", start_barrier )
            print("======= Start Door Found =========")
        elseif string.lower(string.Left(v:GetName(),4)) == "grid" then
            grid_barrier = v
            SetGlobalEntity ( "grid_barrier", grid_barrier )
            print("======= Grid Door Found =========")
        end
    end
end
--hook.Add("InitPostEntity", "FindDoors", scheduler:FindDoors())

function CounterTick()

    PhaseCounter = PhaseCounter - 1
    
    if PhaseCounter <= 0 then
        if Phase == SR_WAITING then
			--if team.NumPlayers(TEAM_ENTRANT) >= 1 then
				scheduler:StartRacing()
			--else
			--	print("======= no racers on start line =========")
			--	scheduler:StartWaiting()
			--	print("------- returning to build mode ---------")
			--end
                        -- Removed - this is not reliable across all types of map,
                        --  such as maps that are timed around the round length
	elseif Phase == SR_RACING then
	
		KillTeam(TEAM_RACER)
			
	    scheduler:StartWaiting()
	end
    end

    scheduler:UpdateUmsg()
    
    --return 1

end


function KillTeam(Team_Name)
	for k,v in pairs(team.GetPlayers(Team_Name)) do
	    --v:ConCommand( "kill_player" ) --NOOO, much much much simpler:
            v:Kill()
	end

end

function scheduler:StartRacing()

    Phase = SR_RACING
    PhaseCounter = SR_RACE_TIME
    PhaseLength = SR_RACE_TIME
    StartTime = SysTime()
    self:UpdateUmsg()
    
    if timer.IsTimer("UpdateSplit") then
        timer.Start("UpdateSplit")
    else
        timer.Create("UpdateSplit", 0.25, 0, UpdateSplit)
    end
    
    if GetConVarNumber("sr_debug") == 1 then print("======= Race! =========") end
    
    for k,v in pairs(player.GetAll()) do
        if v:Team() == TEAM_ENTRANT then
            showMessage(v, "GO!")
        end
    end
        
    if start_barrier then
        start_barrier:Fire("Unlock")
        start_barrier:Fire("Open")
        timer.Simple(2,function() start_barrier:Fire("Lock") end)
        if GetConVarNumber("sr_debug") == 1 then print("Start Door Opened") end
    end
    if grid_barrier then
        grid_barrier:Fire("Unlock")
        grid_barrier:Fire("Close")
        timer.Simple(2,function() grid_barrier:Fire("Lock") end)
        if GetConVarNumber("sr_debug") == 1 then print("Grid Door Closed") end
    end

end

function scheduler:StartWaiting()
    Phase = SR_WAITING
    PhaseCounter = SR_WAIT_TIME
    PhaseLength = SR_WAIT_TIME
    StartTime = 0
    self:UpdateUmsg()
    
    timer.Stop("UpdateSplit")
    
    if IsEntity(start_barrier) then
        start_barrier:Fire("Unlock")
        start_barrier:Fire("Close")
        timer.Simple(2,function() start_barrier:Fire("Lock") end)
        if GetConVarNumber("sr_debug") == 1 then print("Start Door Closed") end
    else
        print("No Start Door")
    end
    if IsEntity(grid_barrier) then
        grid_barrier:Fire("Unlock")
        grid_barrier:Fire("Open")
        --timer.Simple(2,function() grid_barrier:Fire("Lock") end)
        if GetConVarNumber("sr_debug") == 1 then print("Grid Door Opened") end
    else
        print("No Grid Door")
    end
end